package com.example.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.edu.entity.Teacher;
import com.example.edu.mapper.TeacherMapper;
import com.example.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.utils.QueryTeacher;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-22
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public void pageQuery(Page<Teacher> pageParam, QueryTeacher queryTeacher) {

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        String name = queryTeacher.getName();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        Integer level = queryTeacher.getLevel();
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        String begin = queryTeacher.getBegin();
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        
        String end = queryTeacher.getEnd();
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_modified", end);
        }
        queryWrapper.orderByDesc("gmt_create");
        teacherMapper.selectPage(pageParam, queryWrapper);
    }

    @Cacheable(key = "'teacher'",value = "teacherList")
	@Override
	public List<Teacher> selectAllTeacher() {
		// TODO Auto-generated method stub
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.orderByDesc("id");
        teacherQueryWrapper.last("limit 4");
        List<Teacher> teacherList = baseMapper.selectList(teacherQueryWrapper);
		return teacherList;
	}
}
